package com.kwk.messagepushding.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;

/**
 * @Description 时间工具类
 * @Author zeo
 * @Date 2023/3/15 9:42
 */
public class DateUtils {

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前日期
     *
     * @return Date() 当前日期
     */
    public static String getNowDate(String dateType) {
        return "ymd".equals(dateType)
                ? new SimpleDateFormat(YYYY_MM_DD).format(new Date())
                : new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(new Date());
    }

    /**
     * 根据传入的date格式化日期,返回日期字符串
     *
     * @param dateType
     * @param date
     * @return
     */
    public static final String getDateByData(String dateType, Date date) {
        return "ymd".equals(dateType)
                ? new SimpleDateFormat(YYYY_MM_DD).format(date)
                : new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(date);
    }


    /**
     * 获取昨日日期
     *
     * @return
     */
    public static String getYesterdayDate() {
        SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
        Date date = new Date(System.currentTimeMillis() - 1000 * 3600 * 24);
        return formatter.format(date);
    }

    public static String getTomorrowDate() {
        SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
        Date date = new Date(System.currentTimeMillis() - 1000 * 3600 * 24);
        return formatter.format(date);
    }


    /**
     * 获取今天日期
     *
     * @return
     */
    public static String getCurrentDate() {
        SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
        Date date = new Date(System.currentTimeMillis());
        return formatter.format(date);
    }

    /**
     * 当前月份加上指定月份
     * @param num
     * @return
     */
    public static String getBeforeMonth(int num) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = dateFormat.parse(getCurrentDate());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(calendar.MONTH, num);
            date = calendar.getTime();
            return dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 当前日期加上指定日期
     * @param num
     * @return
     */
    public static String getAfterDay(int num) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = dateFormat.parse(getCurrentDate());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(calendar.DATE, num);
            date = calendar.getTime();
            return dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取两个日期之间的所有日期 (年月日)
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getBetweenDate(String startTime, String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 对比时间
     * */
    public static Boolean CompareTime(String time1,String time2){
        String[] arr1 = time1.split(":");
        String[] arr2 = time2.split(":");

        int hour1 = Integer.parseInt(arr1[0]);
        int minute1 = Integer.parseInt(arr1[1]);


        int hour2 = Integer.parseInt(arr2[0]);
        int minute2 = Integer.parseInt(arr2[1]);

        int timeInMinutes1 = hour1 * 60 + minute1;
        int timeInMinutes2 = hour2 * 60 + minute2;

        if (timeInMinutes1 > timeInMinutes2) {
            return true;
        }
        return false;
    }

    /**
     * 时间相减
     * */
    public static int getMinusTime(String time1,String time2){
        String[] arr1 = time1.split(":");
        String[] arr2 = time2.split(":");

        int hour1 = Integer.parseInt(arr1[0]);
        int minute1 = Integer.parseInt(arr1[1]);


        int hour2 = Integer.parseInt(arr2[0]);
        int minute2 = Integer.parseInt(arr2[1]);

        int timeInMinutes1 = hour1 * 60 + minute1;
        int timeInMinutes2 = hour2 * 60 + minute2;

        int res = timeInMinutes2 - timeInMinutes1;

        return res * 60;
    }

    /**
     * 判断休息时间
     * */
    public static String[] getBidianshijian(String time1, String time2, String[] array){
        String[] arr1 = time1.split(":");
        String[] arr2 = time2.split(":");

        int hour1 = Integer.parseInt(arr1[0]);
        int minute1 = Integer.parseInt(arr1[1]);


        int hour2 = Integer.parseInt(arr2[0]);
        int minute2 = Integer.parseInt(arr2[1]);

        int timeInMinutes1 = hour1 * 60 + minute1;
        int timeInMinutes2 = hour2 * 60 + minute2;

        String[] returnArr = new String[array.length];

        for (int i = 0;i < array.length;i++){
            if(timeInMinutes1-timeInMinutes2 >= Integer.parseInt(array[i])){
                returnArr[i] = array[i];
            }
        }

        //去掉null值
        ArrayList<String> temp = new ArrayList<>();

        for (String element : returnArr) {
            if (element != null) {
                temp.add(element);
            }
        }

        String[] result = temp.toArray(new String[0]);

        return result;
    }
    /**
     * 获取当前时间
     * */
    public static String getNowTime(){

        LocalDate currentDate = LocalDate.now(); // 获取当前日期
        LocalTime currentTime = LocalTime.now(); // 获取当前时间

        int hour = currentTime.getHour(); // 获取当前小时
        int minute = currentTime.getMinute(); // 获取当前分钟

        return hour+":"+minute;
    }

    public static void main(String[] args) throws Exception{
        int a = 100;
        int[] b = {1,2,3};
        System.out.println(b.toString());
    }
}
